//    Time Surfer
//    Copyright (C) 2008  Gaian Mystery School
//    http://www.gaianmysteryschool.com
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>

#import "TSCalendarRound.h"

@implementation TSCalendarRound

static inline double radians(double degrees) { return degrees * M_PI / 180; }

void addOvalToPath(CGContextRef c, CGPoint center, float a, float b, float start_angle, float arc_angle, int pie)
{
	float CGstart_angle = 90.0 - start_angle;
	CGContextSaveGState(c);
	CGContextTranslateCTM(c, center.x, center.y);
	CGContextScaleCTM(c, a, b);
	if (pie) 
		CGContextMoveToPoint(c, 0, 0);
	else 
		CGContextMoveToPoint(c, cos(radians(CGstart_angle)), sin(radians(CGstart_angle)));

	CGContextAddArc(c, 0, 0, 1,	radians(CGstart_angle),	radians(CGstart_angle - arc_angle),	arc_angle>0 ? 1 : 0);
	if (pie) 
		CGContextClosePath(c);
	CGContextRestoreGState(c);
}

void fillOval(CGContextRef c, CGRect rect, float start_angle, float arc_angle)
{
	float a, b;
	CGPoint center;
	CGContextBeginPath(c);
	center = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
	a = CGRectGetWidth(rect) / 2;
	b = CGRectGetHeight(rect) / 2;
	addOvalToPath(c, center, a, b, start_angle, arc_angle, 1);
	CGContextClosePath(c);
	CGContextFillPath(c);
}


void strokeOval(CGContextRef c, CGRect rect, float start_angle, float arc_angle)
{
	float a, b;
	CGPoint center;
	CGContextBeginPath(c);
	center = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
	a = CGRectGetWidth(rect) / 2;
	b = CGRectGetHeight(rect) / 2;
	addOvalToPath(c, center, a, b, start_angle, arc_angle, 0 );
	CGContextStrokePath(c);
}


- (id)initWithFrame:(NSRect)frameRect
{
	if ((self = [super initWithFrame:frameRect]) != nil) {
		// Add initialization code here
	}
	return self;
}


-(void)longCount:(int)baktun katun:(int)katun tun:(int)tun uinal:(int)uinal kin:(int)kin
{
	_baktun=baktun;
	_katun=katun;
	_tun=tun;
	_uinal=uinal;
	_kin=kin;
}


- (void)drawRect:(NSRect)rect
{
	NSGraphicsContext *nsContext = [NSGraphicsContext currentContext];
    CGContextRef ctx = [nsContext graphicsPort];

	CGContextSetShouldAntialias(ctx, YES);
    CGContextSetLineWidth(ctx, 8.0);
	CGRect cgRect;
	int end=(_baktun==13 && _katun==0 && _tun==0 && _uinal==0 && _kin==0);
	
	// **** baktun
	cgRect.origin.x=rect.origin.x+10;
	cgRect.origin.y=rect.origin.y+10;
	cgRect.size.width=rect.size.width-20;
	cgRect.size.height=rect.size.height-20;

    CGContextSetRGBStrokeColor(ctx, 0.4,0.7,0.7,1);
	strokeOval(ctx, cgRect, -90, 360);

    CGContextSetRGBStrokeColor(ctx, 0.714, 0.31, 0.0, 1);
	int factor=360/13;
	if (_baktun==13 || end)
		strokeOval(ctx, cgRect, -90, 360);
	else
		strokeOval(ctx, cgRect, -90, _baktun*factor);

	// **** katun
	cgRect.origin.x=rect.origin.x+20;
	cgRect.origin.y=rect.origin.y+20;
	cgRect.size.width=rect.size.width-40;
	cgRect.size.height=rect.size.height-40;
	
    CGContextSetRGBStrokeColor(ctx, 0.4,0.7,0.7,1);
	strokeOval(ctx, cgRect, -90, 360);

    CGContextSetRGBStrokeColor(ctx, 0.754, 0.35, 0.1, 1);
	factor=360/19;
	if (_katun==19 || end)
		strokeOval(ctx, cgRect, -90, 360);
	else
		strokeOval(ctx, cgRect, -90, _katun*factor);


	// **** tun
	cgRect.origin.x=rect.origin.x+30;
	cgRect.origin.y=rect.origin.y+30;
	cgRect.size.width=rect.size.width-60;
	cgRect.size.height=rect.size.height-60;

    CGContextSetRGBStrokeColor(ctx, 0.4,0.7,0.7,1);
	strokeOval(ctx, cgRect, -90, 360);
	
    CGContextSetRGBStrokeColor(ctx, 0.814, 0.45, 0.2, 1);
	factor=360/19;
	if (_tun==19 || end)
		strokeOval(ctx, cgRect, -90, 360);
	else
		strokeOval(ctx, cgRect, -90, _tun*factor);

	// **** uinal
	cgRect.origin.x=rect.origin.x+40;
	cgRect.origin.y=rect.origin.y+40;
	cgRect.size.width=rect.size.width-80;
	cgRect.size.height=rect.size.height-80;
	
    CGContextSetRGBStrokeColor(ctx, 0.4,0.7,0.7,1);
	strokeOval(ctx, cgRect, -90, 360);

    CGContextSetRGBStrokeColor(ctx, 0.990, 0.65, 0.25, 1);
	factor=360/17;
	if (_uinal==17 || end)
		strokeOval(ctx, cgRect, -90, 360);
	else
		strokeOval(ctx, cgRect, -90, _uinal*factor);

	// **** kin
	cgRect.origin.x=rect.origin.x+50;
	cgRect.origin.y=rect.origin.y+50;
	cgRect.size.width=rect.size.width-100;
	cgRect.size.height=rect.size.height-100;

    CGContextSetRGBStrokeColor(ctx, 0.4,0.7,0.7,1);
	strokeOval(ctx, cgRect, -90, 360);
	
    CGContextSetRGBStrokeColor(ctx, 0.995, 0.75, 0.35, 1);
	factor=360/19;
	if (_kin==19 || end)
		strokeOval(ctx, cgRect, -90, 360);
	else
		strokeOval(ctx, cgRect, -90, _kin*factor);




	// **** sun
	cgRect.origin.x=rect.origin.x+60;
	cgRect.origin.y=rect.origin.y+60;
	cgRect.size.width=rect.size.width-120;
	cgRect.size.height=rect.size.height-120;

	// to find divide rgb value with 256
    CGContextSetRGBFillColor(ctx, 0.98, 0.96, 0.101, 1);
	fillOval(ctx, cgRect, 0, 360);


}

@end
